Borland Online And The Cobb Group Present:


November, 1995 - Vol. 2 No. 11

New Borland Product - Tracking bugs with CodeGuard

The C++ programming language brings unprecedented power to software development with its rich set of features and expressive language constructs. Unfortunately, a C++ program can also harbor some of the most obscure bugs you can imagine.

To make matters worse, writing Window programs can cost you endless hours of debugging as you try to locate invalid parameters that you're passing to various Windows Application Programming Interface (API) functions. Similarly, if you allocate Windows resources in your program but neglect to release them properly, your program will exhibit a "resource leak." Slowly but surely, your users will notice their systems slowing to a crawl as Windows begins competing with your application for the resources it uses to manage the environment.

For several years, Windows programmers have written their own tools to combat these problems. Some of these home-grown applications work well; others don't. Unfortunately, it can be difficult detecting if an error reported by such a tool is a "real" er-ror or a bug in the tool itself.

Appropriately, several companies have developed third-party tools and libraries that you can use to test your applications for these types of errors. In this article, we'll examine CodeGuard, a new debugging tool from Borland, which you can use to identify many common Windows programming errors.

What it is

First, you should know that CodeGuard will install only when you're running Borland C++ 4.5, 4.51, or 4.52. (Version 4.51 is a maintenance release that fixes several subtle bugs in 4.5. Version 4.52 is a minor update of 4.51 that includes the Windows 95 header files and corresponding libraries. If you're running version 4.5, the CodeGuard package contains a patch program that will temporarily upgrade your installation to version 4.51.) When you finish installing CodeGuard, your system will be running version 4.53 of Borland C++.

If you're wondering why it's important for you to have a specific version of Borland C++, you need to consider what the CodeGuard installation program places on your hard disk. Primarily, CodeGuard works by providing debugging versions of the C and C++ allocation functions, by intercepting and monitoring many Run-time Library (RTL) and Windows API calls, and by inserting additional debugging code in locations where your program references a data structure such as an object or array.

Obviously, Borland needed to construct and supply compatible versions of the RTL and the ObjectWindows Library (OWL), as well as new code to help the compiler insert the proper debugging information. As Figure A shows, you can control whether the CodeGuard installation program adds these new components to your system.

Figure A - When you install CodeGuard, you can determine which files and libraries it will install when it upgrades your system.

What it isn't

Unlike SmartHeap and some other third- party heap-management tools, CodeGuard doesn't provide higher performance allocation functions than the standard RTL functions. Borland has focused CodeGuard's features directly on debugging 16-bit Windows applications. (Currently, you can't use CodeGuard to help debug 32-bit Windows applications, 32-bit extended DOS applications, or PowerPack for DOS applications.)

What it does

In the October 1995 issue of Borland C++ Developer's Journal, we described how you could create debugging versions of the new() and delete() operators to catch many C++ allocation and deallocation problems (Debugging dynamic memory allocations in C++.) In fact, what we provided was a simple implementation of a number of CodeGuard's capabilities.


Figure B - To enable CodeGaurd for your application, you'll select the Code Gaurd check box in the TargetExpert dialog box.

However, CodeGuard goes far beyond the simple allocation tracking device that we demonstrated­­it goes on to monitor RTL and Windows API calls. In addition, Borland has integrated CodeGuard into the IDE in two ways: You enable CodeGuard by using the TargetExpert dialog box (as shown in Figure B), and you configure CodeGuard's reporting using the configuration IDE tool (as shown in Figure C).

Figure C - You'll use the CodeGuard IDE tool to configure CodeGuard's reporting for the current project.

Up to this point, you may think that CodeGuard simply represents an integrated version of current third-party products such as Bounds-Checker for Windows. After all, there is considerable overlap between the feature sets of these products.

Where CodeGuard begins to diverge from the other products is one of its most fundamental advantages: It can change the way the compiler generates the code that accesses objects or calls member functions. For example, you can configure CodeGuard to verify that an object is valid every time you call one of the member functions of that object. Other tools can only report that an object is invalid when you pass the address of the object to an RTL or API function.


Figure D - If you're debugging from the IDE, CodeGuard throws exceptions that the IDE can catch and display.

Once you've built an application using the CodeGuard libraries, it will report error information to you when you're using the IDE's integrated debugging capabilities or when you're testing the application as a standalone program. For example, if you're debugging a program from the IDE, and the CodeGuard libraries detect an error, you'll see a dialog box similar to the one shown in Figure D. If instead you're running the application as a standalone program, you'll see a dialog box similar to the one that appears in Figure E when CodeGuard detects an error.


Figure E - If CodeGuard is running standalone, it displays a dialog box to inform you of the first error only.

Even though CodeGuard performs error checking that's impossible for a third-party tool such as Bounds-Checker to perform, you'll probably want to use both tools to test your applications. Bounds-Checker is a mature product that provides much broader coverage of Windows API calls than CodeGuard currently does. In addition, the latest version of Bounds-Checker monitors VBX interface calls and provides a detailed trace of your application's messages, API calls, and other events.

How to get it

CodeGuard retails for $99 and requires Borland C++ version 4.5 or higher. You can order CodeGuard directly from Borland by calling 800-331-0877. (Bounds-Checker retails for $249 and isn't specific to any version of Borland C++. You can order Bounds-Checker directly from Nu-Mega Technologies by calling 800-468-6342.)

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.